### Importing packages and Reading in Data
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tm)
## Loading required package: NLP
## 
## Attaching package: 'NLP'
## 
## The following object is masked from 'package:ggplot2':
## 
##     annotate
library(quanteda)
## Package version: 3.2.4
## Unicode version: 14.0
## ICU version: 70.1
## Parallel computing: 8 of 8 threads used.
## See https://quanteda.io for tutorials and examples.
## 
## Attaching package: 'quanteda'
## 
## The following object is masked from 'package:tm':
## 
##     stopwords
## 
## The following objects are masked from 'package:NLP':
## 
##     meta, meta<-
library(tidytext)
library(readr)
library(dplyr)
library(devtools)
## Loading required package: usethis
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.2, released 2022/03/08
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: FALSE 
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-7
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(stringr)
library(ggmap)
## ℹ Google's Terms of Service: <]8;;https://mapsplatform.google.comhttps://mapsplatform.google.com]8;;>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(readr)
library(RColorBrewer)
library(htmlwidgets)
library(leaflet)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggmap':
## 
##     wind
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(usmap)
library(colorspace)
library(leafgl)

endangered_species_og <- read.csv('../IUCN_final.csv')
### Exploring data
class(endangered_species_og$decimalLatitude)
## [1] "numeric"
class(endangered_species_og$decimalLongitude)
## [1] "numeric"
names(endangered_species_og)
##  [1] "gbifID"                           "datasetKey"                      
##  [3] "occurrenceID"                     "kingdom"                         
##  [5] "phylum"                           "class"                           
##  [7] "order"                            "family"                          
##  [9] "genus"                            "species"                         
## [11] "infraspecificEpithet"             "taxonRank"                       
## [13] "scientificName"                   "verbatimScientificName"          
## [15] "verbatimScientificNameAuthorship" "countryCode"                     
## [17] "locality"                         "stateProvince"                   
## [19] "occurrenceStatus"                 "individualCount"                 
## [21] "publishingOrgKey"                 "decimalLatitude"                 
## [23] "decimalLongitude"                 "coordinateUncertaintyInMeters"   
## [25] "coordinatePrecision"              "elevation"                       
## [27] "elevationAccuracy"                "depth"                           
## [29] "depthAccuracy"                    "eventDate"                       
## [31] "day"                              "month"                           
## [33] "year"                             "taxonKey"                        
## [35] "speciesKey"                       "basisOfRecord"                   
## [37] "institutionCode"                  "collectionCode"                  
## [39] "catalogNumber"                    "recordNumber"                    
## [41] "identifiedBy"                     "dateIdentified"                  
## [43] "license"                          "rightsHolder"                    
## [45] "recordedBy"                       "typeStatus"                      
## [47] "establishmentMeans"               "lastInterpreted"                 
## [49] "mediaType"                        "issue"                           
## [51] "iucn_category"                    "X"                               
## [53] "X.1"                              "X.2"                             
## [55] "X.3"                              "X.4"                             
## [57] "X.5"                              "X.6"                             
## [59] "X.7"                              "X.8"                             
## [61] "X.9"                              "X.10"                            
## [63] "X.11"                             "X.12"                            
## [65] "X.13"                             "X.14"                            
## [67] "X.15"                             "X.16"                            
## [69] "X.17"                             "X.18"                            
## [71] "X.19"                             "X.20"                            
## [73] "X.21"                             "X.22"                            
## [75] "X.23"                             "X.24"                            
## [77] "X.25"                             "X.26"                            
## [79] "X.27"                             "X.28"                            
## [81] "X.29"                             "X.30"                            
## [83] "X.31"                             "X.32"                            
## [85] "X.33"                             "X.34"                            
## [87] "X.35"                             "X.36"                            
## [89] "X.37"                             "X.38"                            
## [91] "X.39"                             "X.40"                            
## [93] "X.41"                             "X.42"                            
## [95] "X.43"                             "X.44"                            
## [97] "X.45"                             "X.46"
unique(endangered_species_og$iucn_category)
## [1] "VU" "CR" "EN" "NT" "EX"
### Cleaning dataframe for points exclusively in mainland US
endangered_species <- endangered_species_og %>%
  filter(between(abs(decimalLatitude), 24, 49) & between(abs(decimalLongitude), 66, 124)) %>%
  filter(between(year, 2005, 2022)) %>%
  select(-c("X", "X.1","X.2", "X.3", "X.4", "X.5", "X.6", "X.7", "X.8", "X.9", "X.10", "X.11", "X.12", "X.13", "X.14", "X.15", "X.16", "X.17", "X.18", "X.19", "X.20", "X.21", "X.22", "X.23", "X.24", "X.25", "X.26", "X.27", "X.28", "X.29", "X.30", "X.31", "X.32", "X.33", "X.34", "X.35", "X.36", "X.37", "X.38", "X.39", "X.40", "X.41", "X.42", "X.43", "X.44", "X.45", "X.46" ))
### Endangered_species data broken up by year
endangered_species_2005 <- endangered_species %>%
  filter(year == 2005)
### Establishing color palette for the IUCN Categories
pal <- colorFactor(palette = hcl.colors(5, 'Fall'), 
                   domain = endangered_species$iucn_category)

color_iucn <- pal(endangered_species$iucn_category)
### Creating point data of endangered species layered by year, distinguished by IUCN category
map <- leaflet() %>%
  addTiles('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png') %>%
  addCircles(group = "2005",
             data = endangered_species_2005,
             lng = ~decimalLongitude, lat = ~decimalLatitude,
             weight = 1, color = color_iucn, 
             popup = paste("Species: ", endangered_species$species, "<br/>",
                           "IUCN Category: ", endangered_species$iucn_category)) %>%
  addLegend(pal = pal, values = endangered_species$iucn_category, title = "IUCN Category") 

map

addCircles(group = “2009 - 2012”, data = subset(endangered_species, year >= 2009 & year < 2013 ), lng = ~decimalLongitude, lat = ~decimalLatitude, weight = 1, color = color_iucn) %>% addCircles(group = “2013 - 2016”, data = subset(endangered_species, year >= 2012 & year < 2017), lng = ~decimalLongitude, lat = ~decimalLatitude, weight = 1, color = color_iucn) %>% addCircles(group = “2017 - 2020”, data = subset(endangered_species, year >= 2017 & year < 2021), lng = ~decimalLongitude, lat = ~decimalLatitude, weight = 1, color = color_iucn) %>% addCircles(group = “2021 - 2022”, data = subset(endangered_species, year >= 2021), lng = ~decimalLongitude, lat = ~decimalLatitude, weight = 1, color = color_iucn) %>% addLayersControl(overlayGroups = c(‘2005 - 2008’, ‘2009 - 2012’, ‘2013 - 2016’, ‘2017 - 2020’, ‘2021 - 2022’), options = layersControlOptions(collapsed = TRUE))